/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Rit;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import persistence.SessionManager;

/**
 *
 * @author Johan
 */
public class StartupController extends HttpServlet {

    @Override
    public void init() throws ServletException {
        //hier kunnen alle initiele variabelen ed. worden neergezet 
        //waarschijnlijk ook de sessionfactory voor hibernate zodat 
        //je deze niet elke keer hoeft aan te roepen
        //private static SessionFactory sessionFactory;
        
                
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //String hql = "from Rit where RIT_DELETED = false AND RIT_DATUM >= current_date ORDER BY RIT_DATUM asc, RIT_TIJD asc";

        Session session = null;
        session = SessionManager.openSession();

        Transaction tx = session.beginTransaction();
        //List<Rit> ritten = session.createQuery(hql).list();
        Query q = session.createQuery("from Rit where RIT_DELETED = false AND RIT_DATUM >= current_date ORDER BY RIT_DATUM asc, RIT_TIJD asc");
        q.setMaxResults(5);
        List<Rit> ritten = q.list();
        
        // Array(list) meesturen naar rittenoverzicht 
        request.setAttribute("ritten", ritten);
        request.setAttribute("aantalRitten", ritten.size());
        System.out.println("aantal ritten = " + ritten.size());

        // Hier volgt de 5 bovenknoppen active array, in gebruik in include/top_nav.jsp
        Boolean[] topNavActive = new Boolean[] {
            true, false, false, false, false };
        request.setAttribute("topNavActive", topNavActive);
        
        tx.commit();
        session.close();

        String address = "/index.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}
